import pagination from '@/views/pagination' //分页组件
import agentHttp from '@/utils/agentHttp.js'
import {
  debounce,
  timestampToTime,
  toggle_button_type
} from '@/utils/index.js'
import Edit from "./Popup/edit.vue"; //上游编辑组件
import ContactsTable from "./Popup/ContactsTable.vue"; // 上游联系人列表组件
import ContactsManage from "./Popup/ContactsManage.vue" //联系人编辑组件
import exportExcel from '@/utils/exportExcel' //导出
import {
  confirm
} from '@/utils/confirm' //二次确认组件
import countySelect from '@/views/countySelect/index.vue' //省市区 
export default {
  name: 'priceAudit',
  mounted() {
    this.seachData();
    this.select_get_id();
    this.getProvince();
    
  },
  components: {
    Edit,
    pagination,
    ContactsTable,
    ContactsManage,
    countySelect
  },
  data() {
    return {
      //搜索字段
      searchForm: {
        name: "",
        type: "",
        status: "",
        province: "",
        city: "",
        startDate: "",
        endDate: "",
        createName: "",
        brandName: "",
      },
      //表格字段
      tableData: [],
      exportId: "",
      upTypeMap: {}, //经销商类别  传给上游编辑组件
      dealerArray: [], //所有的主营品牌
      provinceData: [], // 省
      cityData: [], // 市
      selectCode: {
        "启用": true,
        "停用": false
      }, //是否启用
      dialogSendData: { //上游编辑弹窗
        isCode: false, //弹窗是否显示        true是   false否  
        isAdd: false, //弹窗内容是否可编辑
        isMsg: null, //弹窗初始化时 是否发请求        、、默认发  
        id: null,
      },
      contactsData: { //上游联系人列表弹窗
        isCode: false, //弹窗是否显示        true是   false否  
        isAdd: false, //弹窗内容是否可编辑
        isMsg: null, //弹窗初始化时 是否发请求        、、默认发  
        id: null,
      },
      contactsManageData: { //上游联系人编辑弹窗
        isCode: false, //弹窗是否显示        true是   false否  
        isAdd: false, //弹窗内容是否可编辑
        isMsg: null, //弹窗初始化时 是否发请求        、、默认发  
        id: null,
      },
      startDateOpt: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      endDateOpt: {
        disabledDate: (time) => {
          return time.getTime() < new Date(this.searchForm.startDate).getTime();
        }
      },

    }
  },

  methods: {
    seachData() {
      var params = this.searchForm || {};
      params.page = this.tableData.page || 1;
      params.rows = this.tableData.rows || 10;
      agentHttp.postJson(`adminApi/admin/up_supplier/list?page=${params.page}&rows=${params.rows}`, params).then((data) => {
        if (data.success) {
          this.tableData = data.data.tableData;
          this.upTypeMap = data.data.upTypeMap;
        }

      })
    },
    debounceSeach() { //input框内容发生改变时，调接口显示查询结果
      this.tableData.page = 1;
      debounce(this.seachData(this.searchForm), 1000)
    },
    debounceSeachMap() { //省市区的筛选
      this.debounceSeach();
      this.getCity();

    },
    exportExcels(searchForm, url, name) {
      exportExcel(searchForm, url, name)
    },
    //显示上游编辑子组件 
    toggleEdit(isAdd, msg, id) {
      this.dialogSendData.isCode = true;
      this.dialogSendData.isAdd = isAdd;
      this.dialogSendData.isMsg = msg;
      this.dialogSendData.id = id;
    },
    //显示联系人子组件
    ContactsTable(isAdd, msg, id) {
      this.contactsData.isCode = true;
      this.contactsData.isAdd = isAdd;
      this.contactsData.isMsg = msg;
      this.contactsData.id = id;
      this.contactsManageData.id = id;
    },
    //删除
    handleDisabled(index, row) {
      var flag = this.$refs[('flag' + index)].$el.textContent;
      confirm(() => {
        agentHttp.patch('adminApi/admin/up_supplier/disable/' + row.id).then((data) => {
          if (data.success) {
            this.$message.success(data.data || (flag + "成功"));
            this.seachData();
          }
        })
      }, () => {
        this.$message.info('已取消' + flag);
      }, `将${flag}该上游所有相关用途，确认${flag}该条数据吗?`)
    },
    getDate(time) {
      return timestampToTime(time)
    },
    //重置
    resetSearchForm(formName) {
      this.resetFormStatus = true;
      this.tableData.page = 1;
      this.tableData.rows = 10;
      this.$refs[formName].resetFields();
      this.$nextTick(_ => {
        this.resetFormStatus = false;
        this.seachData();
      })
    },
    toggle_button_types(e) {
      return toggle_button_type(e)
    },
    select_get_id(id) { //加载经销商,匹配logo
      agentHttp.get(`adminApi/admin/dictionary/find_all_car_brand`).then((data) => {
        if (data.success) {
          this.dealerArray = data.data

        }
      })
    },
    receiveCityData(province, city, county) { //依次为 省：{id:"",name:""}   市:{id:"",name:""}  区:{id:"",name:""}
      this.searchForm.province = province.id;
      this.searchForm.city = city.id;
      this.searchForm.countyId = county.id;
      this.debounceSeach();
    },
    getProvince() { //获取省
      agentHttp.get('adminApi/admin/dictionary/get_province_list').then((data) => {
        if (data.success) {
          this.provinceData = data.data;
        }
      })
    },
    getCity() { //获取市 
      if( this.searchForm.province){
      agentHttp.get(`adminApi/admin/dictionary/find_city_new/${ this.searchForm.province}`).then((data) => {
        if (data.success) {
          this.cityData = data.data;
        }
      })
    }

    },

  }
}